import * as weixinService from '../services/weixin'
import { USERS_PAGE_SIZE } from '../constants'
import { NEWS_PAGE_SIZE } from '../constants'

export default {
  namespace: 'weixin',
  state: {
    newsList: [],
    newsListByMediaId: {total: 0, items: []},
    newsPage: null,
    newsTotal: null,
    loading: false,
    showModule: 'news',
    openidList: {},
    userInfoList: {user_info_list: []},
    getOpenidListCompleted: false,
  },
  reducers: {
    fetchNewsResult(state, {payload: data}) {
      console.log(data);
      return {...state, newsList: data.items, newsPage: data.page, newsTotal: data.totalCount, loading: false};
    },
    getUsersResult(state, {data}) {
      return {...state, openidList: data};
    },
    getUserInfoListResult(state, {data}) {
      return {...state, userInfoList: data, loading: false};
    },
    fetchNewsByMediaIdResult(state, {data}) {
      return {...state, newsListByMediaId: data, loading: false}
    },
    setLoading(state, {loading}) {
      return {...state, loading: loading};
    },
    selectSideMenu(state, {key}) {
      return {...state, showModule: key};
    },
    getOpenidListCompleted(state, action) {
      return {...state, getOpenidListCompleted: true}
    }
  },
  effects: {
    *fetchNews({ payload }, { call, put }) {
      yield put({type: 'setLoading', loading: true});
      const {data} = yield call(weixinService.fetchNews, payload);
      console.log(data);
      yield put({type: 'fetchNewsResult', payload: {...data, page: payload.page}})
    },
    *getUsers({ payload }, { call, put }) {
      yield put({type: 'setLoading', loading: true});
      let list = [];
      const {data} = yield call(weixinService.getUsers, payload.nextOpenid);
      console.log(data);
      yield put({type: 'getUsersResult', data});
      yield put({type: 'getUserInfoList', openidList: data.data.openid.slice(0, 100)});

      //循环查出所有用户的openid，每次查10000个
      list = list.concat(data.data.openid)
      const count = Math.ceil(data.total / data.count);
      console.log(count)
      let nextOpenid = data.nextOpenid;
      for (let i = 0; i < count - 1; i++) {
        const {data} = yield call(weixinService.getUsers, nextOpenid);
        console.log(data);
        nextOpenid = data.nextOpenid;
        list = list.concat(data.data.openid)
      }
      console.log(list)
      data.data.openid = list;
      yield put({type: 'getUsersResult', data});
      yield put({type: 'getOpenidListCompleted'});

      // yield put({type: 'getUserInfoList', payload: {page: 0, pageSize: USERS_PAGE_SIZE}});
    },
    *getUserInfoList({openidList}, { call, put }) {
      yield put({type: 'setLoading', loading: true});
      const {data} = yield call(weixinService.getUserInfoList, openidList);
      console.log(data);
      yield put({type: 'getUserInfoListResult', data});

    },
    *fetchNewsByMediaId({payload}, {call, put}) {
      yield put({type: 'setLoading', loading: true});
      const {data} = yield call(weixinService.fetchNewsByMediaId, payload);
      console.log(data)
      yield put({type: 'fetchNewsByMediaIdResult', data});
    },
    *sendCustomMsg({payload}, {call, put}) {
      const {data} = yield call(weixinService.sendCustomMsg, payload);
      console.log(data)
    }
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        if (pathname === '/yunyiweilai' || pathname === '/yunyiaoxiang') {
          dispatch({ type: 'fetchNews', payload: {page: 0, pageSize: NEWS_PAGE_SIZE} });
          // dispatch({ type: 'getUsers', payload: {nextOpenid: ''}});
        }
      });
    }
  },
};
